﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using DTO;
using BLL.Store;

public partial class ProductListing : System.Web.UI.UserControl
{

    protected void Page_Init(object sender, EventArgs e)
    {
        this.Page.RegisterRequiresControlState(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int pageSize = Globals.Settings.Store.PageSize;
            if (rblPageIndex.Items.FindByValue(pageSize.ToString()) == null)
                rblPageIndex.Items.Add(new ListItem(pageSize.ToString(), pageSize.ToString()));
            rblPageIndex.SelectedValue = pageSize.ToString();
            gvwProducts.PageSize = pageSize;

            if (!string.IsNullOrEmpty(this.Request.QueryString["DepID"]))
            {
                Department dep = Department.GetDepartmentByID(int.Parse(this.Request.QueryString["DepID"]));
                ddlDepartment.SelectedValue = this.Request.QueryString["DepID"];
                ddlDepartment.DataBind();
            }
            else
            {
                ddlDepartment.DataBind();
                gvwProducts.PageIndex = 0;
                gvwProducts.DataBind();
            }
        }
    }

    protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvwProducts.PageIndex = 0;
        gvwProducts.DataBind();
    }
    protected void gvwProducts_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton btn = e.Row.Cells[7].Controls[0] as ImageButton;
            btn.OnClientClick = "if (confirm('Bạn có chắc muốn xóa sản phẩm này ?') == false) return false;";
        }
    }
    protected void rblPageIndex_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvwProducts.PageSize = int.Parse(rblPageIndex.SelectedValue);
        gvwProducts.PageIndex = 0;
        gvwProducts.DataBind();
    }
    protected void ddlDepartment_DataBinding(object sender, EventArgs e)
    {
        gvwProducts.PageIndex = 0;
        gvwProducts.DataBind();
    }
    protected void btn_themmoi_Click(object sender, EventArgs e)
    {
        Response.Redirect(Globals.ApplicationPath + "Admin/AddEditProduct.aspx");
    }
}
